﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMSoftware.Models;

// TODO: redirect to

namespace RMSoftware.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            DataManager dm = new DataManager();

            ViewData.Model = dm.GetProjects();

            ViewBag.Message = "Список текущих проектов";

            return View();
        }

        [HttpPost]
        public ActionResult CreateProject(Project obj)
        {
            obj.ProjectId = Guid.NewGuid();
            (new DataManager()).SaveProject(obj);
            return RedirectToAction("ProjectDetails", new { id = obj.ProjectId });
        }

        [HttpGet]
        public ActionResult CreateProject()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ProjectDetails(Guid id)
        {
            return View(new DataManager().GetProject(id));
        }

        [HttpGet]
        public ActionResult RiskFactorsList()
        {
            DataManager dm = new DataManager();

            ViewData.Model = dm.RiskFactors;

            return View();
        }

        [HttpPost]
        public ActionResult CreateRiskFactor(Risk obj)
        {
            obj.RiskId = Guid.NewGuid();
            (new DataManager()).AddRiskFactor(obj);
            return RedirectToAction("RiskFactorsList");
        }

        [HttpGet]
        public ActionResult CreateRiskFactor()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DeleteRiskFactor(Guid id)
        {
            (new DataManager()).DeleteRiskFactor(id);

            return RedirectToAction("RiskFactorsList");
        }

        [HttpGet]
        public ActionResult PersonsList()
        {
            DataManager dm = new DataManager();

            ViewData.Model = dm.Persons;

            return View();
        }

        [HttpPost]
        public ActionResult CreatePerson(Person obj)
        {
            obj.PersonId = Guid.NewGuid();
            (new DataManager()).AddPerson(obj);
            return RedirectToAction("PersonsList");
        }

        [HttpGet]
        public ActionResult CreatePerson()
        {
            return View();
        }

        [HttpGet]
        public ActionResult DeletePerson(Guid id)
        {
            (new DataManager()).DeletePerson(id);

            return RedirectToAction("PersonsList");
        }

        [HttpGet]
        public ActionResult CreateRisk(Guid id)
        {
            ProjectRisk pr = new ProjectRisk();
            pr.ProjectId = id;
            pr.RiskId = Guid.Parse("df1c6d17-bf13-4aec-a2d1-46e337ded7c3");

            return View(pr);
        }

        [HttpPost]
        public ActionResult CreateRisk(ProjectRisk obj)
        {
            obj.ProjectRiskId = Guid.NewGuid();
            (new DataManager()).AddRisk(obj);

            return RedirectToAction("ProjectDetails", new { id = obj.ProjectId });
        }

        [HttpGet]
        public ActionResult RiskDetails(Guid id)
        {
            return View((new DataManager()).GetRisk(id));
        }

        [HttpGet]
        public ActionResult DeleteRisk(Guid id)
        {
            DataManager dm = new DataManager();
            ProjectRisk pr = dm.GetRisk(id);
            dm.DeleteRisk(pr);

            return RedirectToAction("ProjectDetails", new { id = pr.ProjectId });
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
